<template>
  <view class="container">
    <!-- 顶部标题 -->
    <view class="header">
      <text class="title">选择学习模式</text>
      <text class="subtitle">选择适合你的学习方式</text>
    </view>

    <!-- 学习模式列表 -->
    <view class="mode-list">
      <!-- 闯关模式 -->
      <view class="mode-item challenge" @tap="goToMode('challenge')">
        <view class="mode-icon">🏆</view>
        <view class="mode-info">
          <text class="mode-title">闯关模式</text>
          <text class="mode-desc">通过关卡挑战提升词汇量</text>
          <view class="mode-stats">
            <text class="stat">当前关卡: 3</text>
            <text class="stat">排名: 28</text>
          </view>
        </view>
        <text class="arrow">→</text>
      </view>

      <!-- 随机记忆 -->
      <view class="mode-item random" @tap="goToMode('random')">
        <view class="mode-icon">🎲</view>
        <view class="mode-info">
          <text class="mode-title">随机记忆</text>
          <text class="mode-desc">随机抽取单词进行记忆</text>
          <view class="mode-stats">
            <text class="stat">{{'今日已学:' + learnedWordsRandom +'词'}}</text>
          </view>
        </view>
        <text class="arrow">→</text>
      </view>

      <!-- 测试模式 -->
      <view class="mode-item test" @tap="goToMode('test')">
        <view class="mode-icon">📝</view>
        <view class="mode-info">
          <text class="mode-title">测试模式</text>
          <text class="mode-desc">检验你的词汇掌握程度</text>
          <view class="mode-stats">
            <text class="stat">{{'正确率:' + correctRateTest + '%'}}</text>
          </view>
        </view>
        <text class="arrow">→</text>
      </view>
    </view>

    <!-- 排行榜 -->
    <view class="leaderboard">
      <view class="leaderboard-header">
        <text class="leaderboard-title">闯关排行榜</text>
        <text class="leaderboard-more" @tap="showFullLeaderboard">查看完整榜单 →</text>
      </view>
      <view class="leaderboard-list">
        <view class="leaderboard-item" v-for="(item, index) in topPlayers" :key="index">
          <text class="rank" :class="{'top3': index < 3}">{{ index + 1 }}</text>
          <image class="avatar" :src="item.avatar" mode="aspectFill"></image>
          <text class="name">{{ item.name }}</text>
          <text class="level">Level {{ item.level }}</text>
          <text class="score">{{ item.score }}分</text>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
import {api} from "@/utils/request";

export default {
  data() {
    return {
      topPlayers: [],
      learnedWordsRandom: 0,
      correctRateTest: 0
    }
  },
  async onShow() {
    const res = await api.getLeaderboard()
    if (res.code === 200) {
      this.topPlayers = []
      res.result.forEach(item => {
        this.topPlayers.push({
          "name": item.username,
          "level": item.level,
          "score": item.point
        })
      })
    }
    await this.getModeMsg()
  },
  onLoad() {
    // 获取选中的词书信息
    const bookInfo = uni.getStorageSync('selectedBookInfo')
    if (bookInfo) {
      this.currentBook = bookInfo
      // 清除存储的信息，避免影响下次选择
      uni.removeStorageSync('selectedBookInfo')
    }
  },
  methods: {
    async getModeMsg(){
      const res0 = await api.getStatisticsByModel(2)
      const res1 = await api.getStatisticsByModel(3)
      if (res0.code === 200){
        this.learnedWordsRandom = res0.result?.errorWord + res0.result?.currentWord
      }
      if (res1.code === 200){
        this.correctRateTest = (((res1.result?.currentWord / (res1.result?.errorWord + res1.result?.currentWord)) ? (res1.result?.currentWord / (res1.result?.errorWord + res1.result?.currentWord)) : 0) * 100).toFixed(1)
      }
    },
    goToMode(mode) {
      switch (mode) {
        case 'challenge':
          uni.navigateTo({url: '/pages/challenge/challenge'})
          break
        case 'random':
          uni.navigateTo({url: '/pages/word-random/word-random'})
          break
        case 'test':
          uni.navigateTo({url: '/pages/word-test/word-test'})
          break
      }
    },
    showFullLeaderboard() {
      uni.navigateTo({url: '/pages/leaderboard/leaderboard'})
    },
  }
}
</script>

<style lang="scss">
.container {
  min-height: 100vh;
  background-image: linear-gradient(to bottom, #f8e5d7, #a8e6e2);
  padding: 40rpx;
  box-sizing: border-box;
}

.header {
  text-align: center;
  margin-bottom: 60rpx;

  .title {
    font-size: 48rpx;
    font-weight: bold;
    color: #333;
    margin-bottom: 10rpx;
  }

  .subtitle {
    font-size: 28rpx;
    color: #666;
  }
}

.mode-list {
  margin-bottom: 60rpx;
}

.mode-item {
  background: rgba(255, 255, 255, 0.9);
  border-radius: 20rpx;
  padding: 30rpx;
  margin-bottom: 30rpx;
  display: flex;
  align-items: center;

  .mode-icon {
    font-size: 60rpx;
    margin-right: 30rpx;
  }

  .mode-info {
    flex: 1;

    .mode-title {
      font-size: 32rpx;
      font-weight: bold;
      color: #333;
      margin-bottom: 10rpx;
    }

    .mode-desc {
      font-size: 24rpx;
      color: #666;
      margin-bottom: 15rpx;
    }

    .mode-stats {
      display: flex;
      gap: 20rpx;

      .stat {
        font-size: 22rpx;
        color: #999;
        background: rgba(0, 0, 0, 0.05);
        padding: 4rpx 12rpx;
        border-radius: 10rpx;
      }
    }
  }

  .arrow {
    font-size: 40rpx;
    color: #999;
  }

  &.challenge {
    border-left: 8rpx solid #ff6b21;
  }

  &.random {
    border-left: 8rpx solid #4CAF50;
  }

  &.test {
    border-left: 8rpx solid #2196F3;
  }
}

.leaderboard {
  background: rgba(255, 255, 255, 0.9);
  border-radius: 20rpx;
  padding: 30rpx;

  .leaderboard-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 30rpx;

    .leaderboard-title {
      font-size: 32rpx;
      font-weight: bold;
      color: #333;
    }

    .leaderboard-more {
      font-size: 24rpx;
      color: #666;
    }
  }

  .leaderboard-list {
    .leaderboard-item {
      display: flex;
      align-items: center;
      padding: 20rpx 0;
      border-bottom: 1px solid rgba(0, 0, 0, 0.05);

      &:last-child {
        border-bottom: none;
      }

      .rank {
        width: 60rpx;
        font-size: 28rpx;
        color: #999;
        text-align: center;

        &.top3 {
          color: #ff6b21;
          font-weight: bold;
        }
      }

      .avatar {
        width: 60rpx;
        height: 60rpx;
        border-radius: 30rpx;
        margin-right: 20rpx;
      }

      .name {
        flex: 1;
        font-size: 28rpx;
        color: #333;
      }

      .level {
        font-size: 24rpx;
        color: #666;
        margin-right: 20rpx;
      }

      .score {
        font-size: 28rpx;
        color: #ff6b21;
        font-weight: bold;
      }
    }
  }
}
</style>